// pages/experience/experience.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    openid: '',
    modalName: null,
    avatarUrl: defaultAvatarUrl,
    tempAvatarUrl: '', 
  },


  top:function(){
    wx.navigateTo({
      url: '../answer_top/answer_top',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */





  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const userProfile = wx.getStorageSync('userProfile');
    const openid = wx.getStorageSync('openid');
    if (!userProfile || !openid) {
      
      this.setData({
        avatarUrl: defaultAvatarUrl,
        nickname: '',
        openid: ''
      });
    } else {
      
      this.setData({
        avatarUrl: userProfile.avatarUrl,
        nickname: userProfile.nickname,
        openid: openid
      });
    }
  },

  onFormSubmit(e) {
    const { nickname } = e.detail.value;  
    const { tempAvatarUrl } = this.data;   
    const openid = wx.getStorageSync('openid'); 
    
    if (!nickname) {
      wx.showToast({
        title: '请先选择昵称',
        icon: 'none'
      });
      return;
    }

    if (!tempAvatarUrl) {
      wx.showToast({
        title: '请先选择头像',
        icon: 'none'
      });
      return;
    }

    // 调用云函数将头像、昵称和 openid 保存到云数据库
    wx.cloud.callFunction({
      name: 'answer_openid', 
      data: {
        avatarUrl: tempAvatarUrl, 
        nickname,
        openid
      },
      success: (cloudRes) => {
        const openid = cloudRes.result.openid;
        const id = cloudRes.result._id
        
        
        wx.setStorageSync('userProfile', { avatarUrl: tempAvatarUrl, nickname });
        wx.setStorageSync('openid', openid);
        
        this.setData({
          avatarUrl: tempAvatarUrl,  
          nickname: nickname,
          openid: openid
        });

        wx.navigateTo({
          url: `/pages/experience/answer/answer?_id=${id}`,
        })

        this.hideModal(); 
      },
      fail: (err) => {
        wx.showToast({
          title: '登录失败',
          icon: 'none'
        });
        console.error(err);
      }
    });
  },

  showModal(e) {
    const { nickname, avatarUrl } = this.data;  
    const openidFromStorage = wx.getStorageSync('openid');  
    
    if (nickname && openidFromStorage) {
      wx.showLoading({
        title: '正在进入...',
      });
  

  
      // 云函数异步执行
      wx.cloud.callFunction({
        name: 'answer_openid', 
        data: {
          avatarUrl: avatarUrl, 
          nickname,
          openid: openidFromStorage  
        },
        success: (cloudRes) => {
          console.log('记录添加成功', cloudRes);
          const id = cloudRes.result._id
          wx.navigateTo({
            url: `/pages/experience/answer/answer?_id=${id}`,
          });
          wx.hideLoading();  
        },
        fail: (err) => {
          console.error('记录添加失败', err);
          wx.hideLoading();  
          wx.showToast({
            title: '记录添加失败',
            icon: 'none'
          });
        }
      });
    } else {
      
      this.setData({
        modalName: e.currentTarget.dataset.target
      });
    }
  },
  
  
  

  // 隐藏模态框
  hideModal(e) {
    this.setData({
      modalName: null
    });
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    this.setData({
      tempAvatarUrl: avatarUrl 
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})